package com.ai.tools;

import com.ai.service.AIService;
import com.common.context.UserContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectService {
    /**
     * 切面 : 设置线程的上下文认证信息
     * 要求 : 函数参数中第一个参数为ToolContext
     * @param joinPoint
     */
    @Before("execution(* com.ai.tools.*.*(..))")
    public void before(JoinPoint joinPoint) {
        ToolContext toolContext = (ToolContext) joinPoint.getArgs()[0];
        String token = (String) toolContext.getContext().get(AIService.AUTHENTICATION_TOKEN);
        Long userId = (Long) toolContext.getContext().get(AIService.USER_ID);
        UserContextHolder.getContext().setUsersToken(token);
        UserContextHolder.getContext().setUsersId(userId);
    }
}